package org.squarecap.apps.eventcompanion.web;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.squarecap.apps.eventcompanion.domain.Curso;
import org.squarecap.apps.eventcompanion.util.Validators;

@RequestMapping("/cursoes")
@Controller
@RooWebScaffold(path = "cursoes", formBackingObject = Curso.class)
public class CursoController {

    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid Curso curso, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        Validators.validateEvento(curso, bindingResult);
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, curso);
            return "cursoes/create";
        }
        uiModel.asMap().clear();
        curso.persist();
        return "redirect:/cursoes/" + encodeUrlPathSegment(curso.getId().toString(), httpServletRequest);
    }

    @RequestMapping(method = RequestMethod.PUT, produces = "text/html")
    public String update(@Valid Curso curso, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        Validators.validateEvento(curso, bindingResult);
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, curso);
            return "cursoes/update";
        }
        uiModel.asMap().clear();
        curso.merge();
        return "redirect:/cursoes/" + encodeUrlPathSegment(curso.getId().toString(), httpServletRequest);
    }
}
